package com.qtjf.qtmoneyservice.view;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.activity.VipRuleActivity;
import com.qtjf.qtmoneyservice.activity.WebViewActivity;
import com.qtjf.qtmoneyservice.adapter.HomeRecyclerAdapter;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.domain.Banner;
import com.qtjf.qtmoneyservice.domain.ResultBanner;
import com.qtjf.qtmoneyservice.domain.ResultProduct;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import me.relex.circleindicator.CircleIndicator;

/**
 * Created by Administrator on 2017/6/20 0020.
 */

public class HomeRecyclerHeadView {
    View mView;
    Context mContext;

    ViewPager viewPager;
    MyViewPagerAdapter adapter;
    List<View> lists;
    CircleIndicator indicator;

    ResultBanner resultBanner;
    Gson gson;
    LinearLayout ruleBt;
    List<Banner> list;

    public HomeRecyclerHeadView(Context mContext) {
        this.mContext = mContext;
    }

    public View getView()
    {
        mView=View.inflate(mContext, R.layout.home_recycler_head_view,null);
        initView();
        initData();
        initEvent();
        return mView;
    }

    private void initView() {
        gson=new Gson();
        viewPager= (ViewPager) mView.findViewById(R.id.viewpager);
        FrameLayout.LayoutParams params= ( FrameLayout.LayoutParams) viewPager.getLayoutParams();
        params.height= Constant.getSCREENWIDTH()/(25/12);
        viewPager.setLayoutParams(params);
        indicator = (CircleIndicator) mView.findViewById(R.id.indicator);
        ruleBt= (LinearLayout) mView.findViewById(R.id.rule_bt);
    }

    private void initData() {
        lists=new ArrayList<View>();
        requestCall();
    }

    private void initEvent() {

        ruleBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.startActivity(new Intent(mContext, VipRuleActivity.class));
            }
        });
    }


    class MyViewPagerAdapter extends PagerAdapter
    {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // super.destroyItem(container, position, object);
            container.removeView(lists.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(lists.get(position));
            lists.get(position).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(mContext,WebViewActivity.class);
                    intent.putExtra("url",list.get(viewPager.getCurrentItem()).getUrl());
                    mContext.startActivity(intent);
                }
            });
            return lists.get(position);
        }
    }


    private void requestCall() {
        String url= Constant.HOMEADDRESS + "index/getBanners";
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Log.d("999",response);
                if(response.length()>25) {
                    resultBanner = gson.fromJson(response, new TypeToken<ResultBanner>() {
                    }.getType());
                    list=resultBanner.getData();
                    if(list!=null&&list.size()>0) {

                        for(int i=0;i<list.size();i++)
                        {
                            View view=View.inflate(mContext,R.layout.home_banner_view,null);
                            final ImageView img= (ImageView) view.findViewById(R.id.banner_img);
                            //绑定数据
                            Glide.with(mContext)
                                    .load(list.get(i).getPic())
              /*  .error(R.mipmap.error_bg)
                .placeholder(R.mipmap.error_bg)*/
                                    .crossFade(1000).into(img);
                            lists.add(view);
                        }

                        adapter=new MyViewPagerAdapter();
                        viewPager.setAdapter(adapter);
                        indicator.setViewPager(viewPager);
                    }
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) {
            // 需要重写获取参数的函数,可以向服务器提交参数
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                return map;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }
}
